<?php
namespace Admin\Model;
use Think\Model;

class FormcontrolModel extends AbstractModel {

	protected $results = array();
	
	public $_validate = array(
        array('FormID','number','请选择表单',Model::MUST_VALIDATE),
        array('IDName','require','请输入控件英文ID',Model::MUST_VALIDATE),
        array('CName','require','请输入控件中文名称',Model::MUST_VALIDATE)
    );
	
	/*
	protected $_auto = array(
    	array('ProjectNO', 'getNowNO',3,'callback')
  	);
    */
	
	public function Myupadd(){
		if(!$this->create()){
			$this->results['msg'] = $this->getError();
			$this->results['state'] = 0;
		}else{
			$res = $this->add();
			if($res) {
				$this->results['state'] = 1;
				$this->results['msg'] = "添加成功！";
				$this->results['id'] = $res;
			}else{
				$this->results['state'] = 0;
				$this->results['msg'] = "添加错误！";
			}
		}
		return $this->results;
	}
	
	
	public function Myupdata(){
		if(!$this->create()){
			$this->results['msg'] = $this->getError();
			$this->results['state'] = 0;
		}else{
			$res = $this->save();
			if($res) {
				$this->results['state'] = 1;
				$this->results['msg'] = "修改成功！";
				$this->results['id'] = $res;
			}else{
				$this->results['state'] = 0;
				$this->results['msg'] = "修改错误！";
			}
		}
		return $this->results;
	}
	
	public function Mydelet(){
		$dbName = $this->getTableName();
		$ID = I("ID");
		$sql = " UPDATE {$dbName} SET IsDel = 1 WHERE ID = {$ID}";
		$res = $this->execute($sql);
		if($res){
			$this->results['state'] = 1;
			$this->results['msg'] = "删除成功！";
			$this->results['id'] = $res;
		}else{
			$this->results['state'] = 0;
			$this->results['msg'] = "删除错误！";
		}
		return $this->results;
	}
	
	public function Myshow($parmArr){
		return $this->field("*")->WHERE($parmArr)->find();
	}
	
	public function MyshowList(){
		$FormID = !empty(I("FormID")) ? (int)I("FormID") : (int)$_GET["FormID"];
		$where = "";
		$inid = "";
		$keyword = I('keyword');
		/*
        if(!empty($keyword)){
			$res = M("form")->field("ID")->where(array("Name"=>array('like','%'.$keyword.'%')))->select();
			foreach($res AS $k => $v){
				$inid .= $v['ID'].",";
			}
			if(!empty($inid)){
				$where = " FormID IN (".substr($inid, 0, -1).")";
			}
			$where .= " OR (a.IDName LIKE '%{$keyword}%' OR a.CName LIKE '%{$keyword}%')";
		}
		*/
		$where .= " a. FormID = {$FormID}";
		if(!empty($keyword)){
			$where .= " AND (a.IDName LIKE '%{$keyword}%' OR a.CName LIKE '%{$keyword}%')";
		}
		$return = array();
		$count = $this->alias('a')->where($where)->count();
	    $page = $this->Mypage($count, 20);
	    $fields = " b.CName AS FormName,a.* ";
	    $join = " LEFT JOIN form AS b ON a.FormID = b.ID ";
		$res = $this->alias('a')->join($join)->where($where)->field($fields)->order("ID DESC")->limit($page->firstRow.','.$page->listRows)->select();
		$return['list'] = $res;
		$return['page'] = $page->show('Admin');
		return $return;
	}
	
	
	public function getFormList(){
		$depat = M("form");
		//$where['IsDel'] = 0;
		return $depat->field('ID,CName')->where()->select();
	}
	
	public function getFormCName(){
		$depat = M("form");
		$ID = !empty(I("FormID")) ? (int)I("FormID") : (int)$_GET["FormID"];
		return $depat->field('ID,CName')->where(array("ID"=>$ID))->find();
	}
	
	public function getFormCtrList(){
		$optionArr = array("隐藏","只读","可写");
		$tr = "";
		$ID = (int)I("ID");
		$processID = (int)I("processID");
		$res = $this->field('*')->where(array("FormID"=>$ID))->select();
		foreach($res AS $k => $v){
			$res1 = M("bp_form_right")->field('*')->where(array("BPID"=>$processID,"ControlID"=>$v['ID']))->find();
			$tr .= "<tr class=\"list-group-item\" id='".$res1['ID']."'><td width=\"80%\">".$v['CName']."</td><td width=\"20%\"><select>";
			foreach($optionArr AS $K => $V){
				$selected = "";
				if($K == $res1['RightType']){
					$selected = "selected";
				}
				$tr .= "<option ".$selected." value ='".$K."'>".$V."</option>";
			}
			if($res1 == NULL){
				$tr .= "<option selected value ='-1' id='".$v['ID']."'></option>";
			}
			$tr .= "</select></td></tr>";
		}
		return $tr;
	}
	
	
}

?>				